package com.cai.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket docket2() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("开发团队-02");
    }

    @Bean
    public Docket docket3() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("开发团队-03");
    }

    // 配置需要实例Docket参考源码
    @Bean
    public Docket docket(Environment environment) {
        // 指定环境开启Swagger
        Profiles profiles = Profiles.of("dev", "test");
        // 判断环境是否存在
        boolean flag = environment.acceptsProfiles(profiles);
        System.out.println(flag);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("开发团队-01")
                .enable(flag) // 关闭swagger
                // RequestHandlerSelectors 配置扫描接口方式
                // .basePackage 扫描包
                // withClassAnnotation 指定注解的类就会被扫描
                // withMethodAnnotation 指定注解的方法就会被扫描
                // 过滤请求 ant指定请求方式仅扫描
                .select().apis(RequestHandlerSelectors.basePackage("com.cai")) //.paths(PathSelectors.ant("/hello"))
                .build();
    }

    // 配置Swagger信息 apiInfo
    private ApiInfo apiInfo() {
        // 从源码拿的
        Contact contact = new Contact("蔡文杰", "https://blog.csdn.net/caiwenjie_____?type=blog", "1257791157@qq.com");
        return new ApiInfo(
                "SwaggerAPI文档",
                "跟着狂神学习Java",
                "1.0",
                "https://blog.csdn.net/caiwenjie_____?type=blog",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
        );
    }
}